/**
 * 
 */
package ar.edu.unq.svnpermission.ui.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.IModel;

import ar.edu.unq.svnpermission.domain.Group;
import ar.edu.unq.svnpermission.panels.LinkPanel;
import ar.edu.unq.svnpermission.ui.model.Groups;

/**
 * @author nicolas
 *
 */
public class GroupsPage extends InternalPage<Groups> {

	private static final long serialVersionUID = 1L;
	private AjaxFallbackDefaultDataTable<Group> groupDataTable;
	ListView<Group> groupsListView;

	@Override
	protected void addFields() {
		this.addGroupTable();		
	}
	

	@Override
	protected void addButtons() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected CompoundPropertyModel<Groups> createModel() {
		return new CompoundPropertyModel<Groups>(new Groups());
	}

	@Override
	protected String getFormName() {
		// TODO Auto-generated method stub
		return "formGroupList";
	}
	
	
	private void addGroupTable()
	{
        List<IColumn<Group>> columns = new ArrayList<IColumn<Group>>();
        columns.add(new PropertyColumn<Group>(new Model<String>("Nombre"), "toString"));      
        columns.add(this.createDeleteColumn());  
        columns.add(this.createEditColumn());
        this.setGroupsTable();
        this.getForm().add(this.groupsListView);	
	}
	
	private void setGroupsTable()
	{
		 groupsListView = new ListView<Group>("groupsList",((Groups)this.getForm().getDefaultModelObject()).getAll()) {

		private static final long serialVersionUID = 1L;

		@Override
		protected void populateItem(final ListItem<Group> item) {
			item.add(new Label("name",item.getModelObject().getKeyName()));
			item.add(new Link<Group>("deleteLink", item.getModel()) {
				private static final long serialVersionUID = 1L;
				public void onClick() {
						new Groups().delete(item.getModelObject());
						GroupsPage.this.getForm().modelChanged();
						setResponsePage(GroupsPage.class);
				    }
				});
				
				item.add(new Link<Group>("editLink", item.getModel()) {
					private static final long serialVersionUID = 1L;

					public void onClick() {
						setResponsePage(new NewGroupPage(item.getModelObject()));
				    }
				});				
				
				
			}
		}; 
		this.getForm().setOutputMarkupId(true);
		this.getForm().add(groupsListView);

	}

	private AbstractColumn<Group> createDeleteColumn()
	{
		return new AbstractColumn<Group>(new Model<String>("Eliminar")) {           
			private static final long serialVersionUID = 1L;
			public void populateItem(Item<ICellPopulator<Group>> item, String id, IModel<Group> model) {  
                LinkPanel<Group> delete = new LinkPanel<Group>(id,model,"Eliminar") {
					private static final long serialVersionUID = 1L;

					@Override 
                    public void onClick(AjaxRequestTarget art) {                        
						Groups groupModel = (Groups)GroupsPage.this.getForm().getDefaultModelObject();
						groupModel.setGroup(this.getModelObject());
						groupModel.delete();
						groupModel.reSetGroupsDataProvider();
						GroupsPage.this.groupDataTable.modelChanged();						
						art.add(GroupsPage.this.groupDataTable);
                    } 
                }; 
                item.add(delete);                
            }  
        };
	}
	
	private AbstractColumn<Group> createEditColumn()
	{
		return new AbstractColumn<Group>(new Model<String>("Editar")) {           
			private static final long serialVersionUID = 1L;
			public void populateItem(Item<ICellPopulator<Group>> item, String id, IModel<Group> model) {  
                LinkPanel<Group> delete = new LinkPanel<Group>(id,model,"Editar") {
					private static final long serialVersionUID = 1L;

					@Override 
                    public void onClick(AjaxRequestTarget art) {                        
						this.setResponsePage(new NewGroupPage(this.getModelObject()));
                    } 
                }; 
                item.add(delete);                
            }  
        };
	}	
	
	public AjaxFallbackDefaultDataTable<Group> getGroupDataTable() {
		return groupDataTable;
	}

	public void setGroupDataTable(AjaxFallbackDefaultDataTable<Group> groupDataTable) {
		this.groupDataTable = groupDataTable;
	}

}
